package com.banzhi.shop.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banzhi.shop.R;
import com.banzhi.shop.adapter.OrderDetailsAdapter;
import com.banzhi.shop.base.BaseActivity;
import com.banzhi.shop.entity.OrderDetails.OrderDetails_data;
import com.banzhi.shop.model.CallBack.OrderDetailsCallBack;
import com.banzhi.shop.model.IOrderDetailsModel;
import com.banzhi.shop.model.Impl.OrderDetailsImpl;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.DateUtil;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.NumberUtils;
import com.banzhi.shop.utils.ShareUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.utils.Translucent;
import com.banzhi.shop.view.ListViewForScrollView;

import org.json.JSONObject;

/**
 * 订单详情
 */
public class OrderDetailsActivity extends BaseActivity implements View.OnClickListener, OrderDetailsCallBack {
    /**
     * 传过来的订单ID
     */
    private String id;
    /**
     * 传过来的订单状态
     */
    private String status;
    /**
     * 传过来的状态，判断是自己的订单还是下线的订单
     */
    private String shoporder;
    /**
     * 退出
     */
    private ImageView ivback;
    private IOrderDetailsModel model;
    /**
     * 实体类
     */
    private OrderDetails_data orderdata;
    /**
     * 名字
     */
    private TextView tvname;
    /**
     * 电话
     */
    private TextView tvphone;
    /**
     * 地址
     */
    private TextView tvaddress;
    /**
     * 总价
     */
    private TextView tvzongjia;
    /**
     * 快递
     */
    private TextView tvkuaidi;
    /**
     * 优惠
     */
    private TextView tvyouhui;
    /**
     * 订单总价
     */
    private TextView tvdingdanzongjia;
    /**
     * 实际付款
     */
    private TextView tvshijifukuan;
    /**
     * 订单状态
     */
    private ImageView ivstatus;
    /**
     * 订单状态
     */
    private TextView tvstatus;
    /**
     * 支付方式
     */
    private TextView tvzhifu;
    /**
     * 支付时间
     */
    private TextView tvzhifutime;
    /**
     * 商品列表
     */
    private ListViewForScrollView listview;
    /**
     * 订单号
     */
    private TextView tvdingdanhao;
    /**
     * 订单创建时间
     */
    private TextView tvtime;
    private TextView tvfuzhi;
    private OrderDetailsAdapter adapter;
    private LinearLayout llzhifu;
    private TextView tvfuzhiwuliu;
    private LinearLayout llpaytime;
    private LinearLayout llwuliuname;
    private TextView tvwuliuname;
    private LinearLayout llwuliunum;
    private TextView tvwuliunum;
    private TextView tvdianzhulijian;
    private RelativeLayout rllijian;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_details);
        Translucent.setTranslucent(this);
        setinits();
        setListeners();
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        status = intent.getStringExtra("status");
        shoporder = intent.getStringExtra("shoperOrder");

        model = new OrderDetailsImpl();
        LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
        model.getOrderData(id, this);
        showViews();
    }

    private void showViews() {
        if ("0".equals(status)) {
            tvstatus.setText("待付款");
            ivstatus.setImageResource(R.mipmap.daifukuan);
            llzhifu.setVisibility(View.GONE);
        } else if ("1".equals(status)) {
            tvstatus.setText("待发货");
            ivstatus.setImageResource(R.mipmap.daifahuo);
        } else if ("2".equals(status)) {
            tvstatus.setText("待收货");
            ivstatus.setImageResource(R.mipmap.daishouhuo);
        } else if ("3".equals(status)) {
            tvstatus.setText("已完成");
            ivstatus.setImageResource(R.mipmap.chenggong);
        } else {
            tvstatus.setText("已关闭");
            tvstatus.setTextColor(Color.GRAY);
        }
    }

    private void setinits() {
        ivback = (ImageView) findViewById(R.id.iv_back);
        tvname = (TextView) findViewById(R.id.tv_name);
        tvphone = (TextView) findViewById(R.id.tv_phone);
        tvaddress = (TextView) findViewById(R.id.tv_address);
        tvzongjia = (TextView) findViewById(R.id.tv_zongjia);
        tvkuaidi = (TextView) findViewById(R.id.tv_kuaidi);
        tvyouhui = (TextView) findViewById(R.id.tv_youhui);
        tvzhifu = (TextView) findViewById(R.id.tv_zhifu);
        tvfuzhiwuliu = (TextView) findViewById(R.id.tv_fuzhiwuliu);
        tvdingdanzongjia = (TextView) findViewById(R.id.tv_dingdanzongjia);
        tvshijifukuan = (TextView) findViewById(R.id.tv_shifukuan);
        listview = (ListViewForScrollView) findViewById(R.id.listview);
        ivstatus = (ImageView) findViewById(R.id.iv_status);
        tvstatus = (TextView) findViewById(R.id.tv_status);
        tvdingdanhao = (TextView) findViewById(R.id.tv_bianhao);
        llzhifu = (LinearLayout) findViewById(R.id.ll_zhifu);
        tvtime = (TextView) findViewById(R.id.tv_time);
        tvfuzhi = (TextView) findViewById(R.id.tv_fuzhi);
        tvzhifutime = (TextView) findViewById(R.id.tv_zhifutime);
        llpaytime = (LinearLayout) findViewById(R.id.ll_paytime);
        tvwuliuname = (TextView) findViewById(R.id.tv_wuliuname);
        llwuliuname = (LinearLayout) findViewById(R.id.ll_wuliuname);
        tvwuliunum = (TextView) findViewById(R.id.tv_wuliunumber);
        tvdianzhulijian = (TextView) findViewById(R.id.tv_dianzhulijian);
        llwuliunum = (LinearLayout) findViewById(R.id.ll_wuliunumber);
        rllijian = (RelativeLayout) findViewById(R.id.rl_lijian);
    }

    private void setListeners() {
        ivback.setOnClickListener(this);
        tvfuzhi.setOnClickListener(this);
        tvfuzhiwuliu.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_fuzhi:
                ShareUtils.copyLianjie(this, tvdingdanhao.getText().toString().trim());
                break;
            case R.id.tv_fuzhiwuliu:
                ShareUtils.copyLianjie(this, tvwuliunum.getText().toString().trim());
                break;
        }
    }

    /**
     * 展示页面
     */
    private void setViews() {
        tvname.setText(orderdata.getAddress().getRealname());
        tvphone.setText(orderdata.getAddress().getMobile());
        tvaddress.setText(orderdata.getAddress().getAddressDetail());
        if (ASUtils.isCanUse(orderdata.getOrderInfos().getPaytype())) {
            tvzhifu.setText(orderdata.getOrderInfos().getPaytype());
        }
        if (ASUtils.isCanUse(orderdata.getOrderInfos().getPriceSum())) {
            tvzongjia.setText(NumberUtils.DecimalFormat(orderdata.getOrderInfos().getPriceSum()));
        }
        if (ASUtils.isCanUse(orderdata.getOrderInfos().getDispatchprice())) {
            tvkuaidi.setText(NumberUtils.DecimalFormat(orderdata.getOrderInfos().getDispatchprice()));
        }
        if (ASUtils.isCanUse(orderdata.getOrderInfos().getDiscountprice())) {
            tvyouhui.setText(NumberUtils.DecimalFormat(orderdata.getOrderInfos().getDiscountprice()));
        }
        if (ASUtils.isCanUse(orderdata.getOrderInfos().getPriceSum())) {
            tvdingdanzongjia.setText(NumberUtils.DecimalFormat(orderdata.getOrderInfos().getPriceSum()));
        }
        if (ASUtils.isCanUse(orderdata.getOrderInfos().getPrice())) {
            tvshijifukuan.setText(NumberUtils.DecimalFormat(orderdata.getOrderInfos().getPrice()));
        }
        if ("1".equals(shoporder)) {
            if (ASUtils.isCanUse(orderdata.getOrderInfos().getCommission())) {
                tvdianzhulijian.setText(NumberUtils.DecimalFormat(orderdata.getOrderInfos().getCommission()));
            }
        } else {
            rllijian.setVisibility(View.GONE);
        }

        if (adapter == null) {
            adapter = new OrderDetailsAdapter(this, orderdata.getOrderGoodsInfo());
            listview.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }

        tvdingdanhao.setText(orderdata.getOrderInfos().getOrdersn());
        if (ASUtils.isCanUse(orderdata.getOrderInfos().getCreatetime())) {
            tvtime.setText(DateUtil.getData(orderdata.getOrderInfos().getCreatetime()));
        }
        if (ASUtils.isCanUse(orderdata.getOrderInfos().getPaytime()) && Integer.parseInt(orderdata.getOrderInfos().getPaytime()) > 0) {
            llpaytime.setVisibility(View.VISIBLE);
            tvzhifutime.setText(DateUtil.getData(orderdata.getOrderInfos().getPaytime()));
        } else {
            llpaytime.setVisibility(View.GONE);
        }
        if (ASUtils.isCanUse(orderdata.getOrderInfos().getExpresscom())) {
            llwuliuname.setVisibility(View.VISIBLE);
            tvwuliuname.setText(orderdata.getOrderInfos().getExpresscom());
        } else {
            llwuliuname.setVisibility(View.GONE);
        }

        if (ASUtils.isCanUse(orderdata.getOrderInfos().getExpresssn())) {
            llwuliunum.setVisibility(View.VISIBLE);
            tvwuliunum.setText(orderdata.getOrderInfos().getExpresssn());
        } else {
            llwuliunum.setVisibility(View.GONE);
        }
    }

    @Override
    public void ongetDataSuccess(Object object) {
        LoadingDialogUtils.dismiss();
        orderdata = (OrderDetails_data) object;
        setViews();
    }


    @Override
    public void ongetDataError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }
}
